<?php
namespace Admin\Controller;

class FileController extends CommonController
{
    var $path;

    //新增、上传文件、下载

    function __construct()
    {
        parent::__construct();

        C("URL_MODEL", 0);
        $path = isset($_GET['path']) ? $_GET["path"] : getcwd() . "/Public";
        $this->path = $path;
        $this->assign("path", $path);
    }

    function index()
    {
        $files = dir_info($this->path);
        $this->assign("files", $files);
        $this->display();
    }


    function edit()
    {
        $this->filename = basename($this->path);
        $this->content = file_get_contents($this->path);
        $this->display();
    }

    function update()
    {
        $oldname = $this->path;
        $newname = dirname($this->path) . '/' . I("post.filename");
        rename($oldname, $newname);
        file_put_contents($newname, I("post.content"));

        $url = U("index", array('path' => dirname($newname)));
        $this->success("修改成功", $url);
    }

    function show()
    {
        $path = explode("/Public", $this->path);
        $image = "/Public" . $path[1];
        $this->assign("image", $image);
        $this->display();
    }

    function destroy()
    {
        if (is_dir($this->path)) {
            rm_dir($this->path);
        } else {
            unlink($this->path);
        }
        $this->success("删除成功", dirname($this->path));
    }

    function add()
    {
        //dump($_POST);

        //$path = $this->path."/";
        //dump($path);exit;
        $this->display();

    }
    function store()
    {
        //dump($_POST);exit;
        //dump($path);exit;
        $filename = dirname($this->path) . '/' . I("post.filename");
        file_put_contents($filename, I("post.content"));

        $url = U("index", array('path' => dirname($filename)));
        $this->success("修改成功", $url);
    }
    function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','txt');// 设置附件上传类型
        $path = $this->path.'/' ;
        //dump ($path);exit;
        $upload->rootPath  =     $path; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            $this->success('上传成功！');
        }
    }
    function destroy_cache()
    {
        $cache_dir=getcwd()."/Application/Runtime/Cache";
        //dump($cache_dir);exit;
        if (is_dir($cache_dir)) {
            rm_dir($cache_dir);
        } else {
            unlink($cache_dir);
        }
        $this->success('清除缓存成功！');
    }



}